var _MoodStorage = require("../../services/MoodStorage.js");
var _timeStamp = require("../../services/timeStamp.js");
var statisticsPageViewModel = null;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    //获取记录集合
    RecordCollection: [],
    //动态加载状态信息
    Status: "",
    Mood:[],    
    moodItem: [],
    ColorList: app.globalData.ColorList,
    year: new Date().getFullYear(),      // 年份
    month: new Date().getMonth() + 1,    // 月份
    day: new Date().getDate(),
    days_color: [],
  },
  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  //查询语句
  _where: null,

  //能否加载更多结果
  _canLoadMore: false,

  //动态加载状态常量
  Loading: "正在载入",
  NoMoreResults: "没有更多结果",
  NoResults: "没有结果",

  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab == e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    statisticsPageViewModel = this;
    this.setData({
      moodItem: app.globalData.moodList
    });
    const days_count = new Date(this.data.year, this.data.month, 0).getDate();
    let days_color = new Array;
    var that = this;
    console.log(this.data.month.toString())
    var month = this.data.month.toString()
    if (this.data.month < 10) {
      month = "0" + month;
    }

    _MoodStorage.getMonthRecordAsync(month, function (data) {

      for (let i = 0; i <= data.length; i++) {
        console.log(_timeStamp.timeStampToDate(data[i].TimeStamp, 'D'));
        days_color.push({
          month: 'current', day: _timeStamp.timeStampToDate(data[i].TimeStamp, 'D'), color: 'white', background: app.globalData.moodList[parseInt(data[i].Mood)].color
        });
        that.setData({
          days_color
        })
      }
      that.setData({
        days_color
      })
    }, function () {

    }
    );
  },
  next: function (event) {
    this.data.month += 1;
    let days_color = new Array;
    var that = this;
    var month = this.data.month.toString()
    if (this.data.month < 10) {
      month = "0" + month;
    }

    _MoodStorage.getMonthRecordAsync(month, function (data) {

      for (let i = 0; i <= data.length; i++) {
        console.log(_timeStamp.timeStampToDate(data[i].TimeStamp, 'D'));
        days_color.push({
          month: 'current', day: _timeStamp.timeStampToDate(data[i].TimeStamp, 'D'), color: 'white', background: app.globalData.moodList[parseInt(data[i].Mood)].color
        });
        that.setData({
          days_color
        })
      }
      that.setData({
        days_color
      })
    }, function () {

    }
    );
    this.setData({
      days_color
    })
  },
  prev: function (event) {
    this.data.month -= 1;
    let days_color = new Array;
    var that = this;
    var month=this.data.month.toString()
    if (this.data.month < 10){
      month="0"+month;
    } 
    
    _MoodStorage.getMonthRecordAsync(month, function (data) {

      for (let i = 0; i <= data.length; i++) {
        console.log(_timeStamp.timeStampToDate(data[i].TimeStamp, 'D'));
        days_color.push({
          month: 'current', day: _timeStamp.timeStampToDate(data[i].TimeStamp, 'D'), color: 'white', background: app.globalData.moodList[parseInt(data[i].Mood)].color
        });
        that.setData({
          days_color
        })
      }
      that.setData({
        days_color
      })
    }, function () {

    }
    );
    this.setData({
      days_color
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var where={};
    this._where=where;
    this.setData({ RecordCollection:[]});
    this._canLoadMore=true;
    this.loadMore();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore();
  },
  loadMore:function(){
    if (!this._canLoadMore) return;
    this.setData({ status: statisticsPageViewModel.Loading });
    _MoodStorage.getDataRocordAsync(this._where, this.data.RecordCollection.length, 10, function (records) {
      statisticsPageViewModel.setData({
        RecordCollection: statisticsPageViewModel.data.RecordCollection.concat(records)
      });
      statisticsPageViewModel.setData({
        status: ""
      });
      if (records.length < 5) {
        statisticsPageViewModel._canLoadMore = false;
        statisticsPageViewModel.setData({ status: statisticsPageViewModel.NoMoreResults });
      }

      if (records.length == 0 && statisticsPageViewModel.RecordCollection.length == 0) {
        statisticsPageViewModel.setData({ status: statisticsPageViewModel.NoResults });
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
